package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.ddyx.entity.GoodsTag;
import com.ddyx.entity.StoreActivity;
import com.ddyx.entity.user.UEmploye;
import com.ddyx.mapper.ActivityGoodsMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.GoodsTagMapper;
import com.ddyx.mapper.StoreActivityMapper;
import com.ddyx.mapper.user.UEmployeMapper;
import com.ddyx.service.IndexService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MQUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.ddyx.vo.IndexBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


@Slf4j
@Service
public class IndexServiceImpl implements IndexService {
	
	
	@Resource
	StoreActivityMapper StoreActivityMapper;
	
	@Resource
	ActivityGoodsMapper activityGoodsMapper;
	
	@Resource
	GoodsMapper goodsMapper;
	
	@Resource
	GoodsTagMapper goodsTagMapper;
	
	@Resource
	UEmployeMapper uEmployeMapper;
	
	@Autowired
	JmsTemplate jmsTemplate;
	
	@Autowired
	Destination topicDestination;
	
	//新品上架
	@Override
	public ResponseForm getNewActivityList(RequestForm param) {
		ResponseForm result = new ResponseForm();
		
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			
			//String storeNo = (String) paramMap.get("storeNo");
			
			//头图
			
			List<StoreActivity>  list =getNewActivityList(paramMap.get("storeNo"), 2, 4);
			
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	
	@Override
	public ResponseForm getData(RequestForm param) {
		ResponseForm result = new ResponseForm();
		IndexBean bean = new IndexBean();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			
			//String storeNo = (String) paramMap.get("storeNo");
			
			//头图
			
			bean.setHeaderActivity(getActivityList(paramMap.get("storeNo"), 2, 1));
			//折扣商品
			bean.setDiscountGoods(getDiscountGoods(paramMap.get("storeNo"), 2, 2));
			//最新活动
			bean.setNewActivity(getNewActivityList(paramMap.get("storeNo"), 2, 4));
			
			//热门分类
			bean.setGoodsTagList(getGoodsTagList(paramMap.get("storeNo")));
			//店长推荐活动活动
			bean.setRecommendActivity(getActivityList(paramMap.get("storeNo"), 2, 3));
			
			result.setData(bean);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	
	//头图
	List<StoreActivity> getActivityList(Object storeNo, Object type, Object category){
		Example example = new Example(StoreActivity.class);
		example.setOrderByClause("sort DESC");
		Criteria c = example.createCriteria();
		c.andEqualTo("storeNo",storeNo);
		c.andEqualTo("type", type);
		c.andEqualTo("category",category);
		c.andEqualTo("status", 0);
		List<StoreActivity> activityList=StoreActivityMapper.selectByExample(example);
		return activityList;
	}
	//新款上架
	List<StoreActivity> getNewActivityList(Object storeNo, Object type, Object category){
		Example example = new Example(StoreActivity.class);
		example.setOrderByClause("sort DESC");
		Criteria c = example.createCriteria();
		c.andEqualTo("storeNo",storeNo);
		c.andEqualTo("type", type);
		c.andEqualTo("category",category);
		c.andEqualTo("status", 0);
		List<StoreActivity> activityList=StoreActivityMapper.selectByExample(example);
		for (int i = 0; i < activityList.size(); i++) {
			StoreActivity activity = activityList.get(i);
			List<Map<String, Object>>  goodsList = goodsMapper.getActivityGoodsList(activity.getId());
			if(goodsList.size()>=3){
				activity.setGoodsList(goodsList.subList(0, 3));
			}else{
				activity.setGoodsList(goodsList);
			}
		}
		
		return activityList;
		
	}
	
	
	// 折扣商品
	List<Map<String, Object>> getDiscountGoods(Object storeNo, Object type, Object category) {

		List<Map<String, Object>> goodsList = new ArrayList<Map<String, Object>>();
		Example example = new Example(StoreActivity.class);
		Criteria c = example.createCriteria();
		c.andEqualTo("storeNo", storeNo);
		c.andEqualTo("type", type);
		c.andEqualTo("category", category);
		c.andEqualTo("status", 0);
		
		List<StoreActivity> list = StoreActivityMapper.selectByExample(example);
		if (list == null || list.size() == 0) {
			return goodsList;
		}
		StoreActivity storeBean = list.get(0);

		goodsList = goodsMapper.getActivityGoodsList(storeBean.getId());

		return goodsList;

	}
	
	// 热门分类
	List<GoodsTag> getGoodsTagList(Object storeNo) {

		Example example = new Example(GoodsTag.class);
		example.setOrderByClause("tag_sort DESC");
		Criteria c = example.createCriteria();
		c.andEqualTo("storeNo", storeNo);
		// c.andEqualTo("type", type);
		// c.andEqualTo("category", category);
		c.andEqualTo("tagStatus", 0);
		List<GoodsTag> list = goodsTagMapper.selectByExample(example);

		return list;

	}

	/**
	 * 支付版-获取首页信息
	 * @param ResponseForm
	 * @return
	 */
	@Override
	public ResponseForm getNewGoodsList(RequestForm param) {
		ResponseForm result = new ResponseForm();
		IndexBean bean = new IndexBean();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
			String employeNo =(String) paramMap.get("employeNo");
			String wxUid =(String) paramMap.get("wxUid");
			
			if(!StringUtils.isNotBlank(storeNo)){
				result.setStatus(false);
				result.setMessage("门店编号有误");
				log.error("门店编号有误");
				return result;
			}
			if(!StringUtils.isNotBlank(employeNo)){
				UEmploye employe = new UEmploye();
				employe.setStoreNo(storeNo);
				employe.setRoleId(1);
				List<UEmploye> eList=uEmployeMapper.select(employe);
				if(eList!=null && eList.size()>0){
					employe = eList.get(0);
					employeNo =employe.getEmployeNo();
				}else{
					employeNo ="0";
				}
			}
			com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
			List<Map<String, Object>> goodsList=goodsMapper.getNewGoodsList(paramMap);
			result.setTotal((int) pageHelper.getTotal());
			result.setData(goodsList);
			
			//打点数据
			if(StringUtils.isNotBlank(wxUid)){
				//新增门店客户
				Map<String, Object> cMap = new HashMap<String, Object>();
				cMap.put("wxUid", wxUid);
				cMap.put("storeNo", storeNo);
				cMap.put("employeNo", employeNo);
				cMap.put("actionType", EventTag.NEWCUSTOMER);
				cMap.put("createTime", new Date());
				MQUtil.sendObMsg(cMap,jmsTemplate,topicDestination);
				//浏览首页
				//Map<String, Object> cMap = new HashMap<String, Object>();
				cMap.put("wxUid", wxUid);
				cMap.put("storeNo", storeNo);
				cMap.put("employeNo", employeNo);
				cMap.put("actionType", EventTag.BROWSE);
				cMap.put("eventTime", new Date());
				MQUtil.sendObMsg(cMap,jmsTemplate,topicDestination);
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	
	}


	@Override
	public ResponseForm getHeaderPic(RequestForm param) {
		ResponseForm result = new ResponseForm();
		IndexBean bean = new IndexBean();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
//			String employeNo =(String) paramMap.get("employeNo");
			
			if(!StringUtils.isNotBlank(storeNo)){
				result.setStatus(false);
				result.setMessage("门店编号有误");
				log.error("门店编号有误");
				return result;
			}
			List<StoreActivity> list= getActivityList(paramMap.get("storeNo"), 2, 1);
			
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	
	
	}


	@Override
	public ResponseForm getDiscountGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		IndexBean bean = new IndexBean();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
//			String employeNo =(String) paramMap.get("employeNo");
			
			if(!StringUtils.isNotBlank(storeNo)){
				result.setStatus(false);
				result.setMessage("门店编号有误");
				log.error("门店编号有误");
				return result;
			}
			List<Map<String, Object>> list= goodsMapper.getDiscountGoodsList(paramMap);
			
			result.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}


	@Override
	public ResponseForm getIndexEmploye(RequestForm param) {

		ResponseForm result = new ResponseForm();
		IndexBean bean = new IndexBean();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
			String employeNo =(String) paramMap.get("employeNo");
			
			if(!StringUtils.isNotBlank(storeNo)){
				result.setStatus(false);
				result.setMessage("门店编号有误");
				log.error("门店编号有误");
				return result;
			}
			if(!StringUtils.isNotBlank(employeNo)){
				UEmploye employe = new UEmploye();
				employe.setStoreNo(storeNo);
				employe.setRoleId(1);
				List<UEmploye> eList=uEmployeMapper.select(employe);
				if(eList!=null && eList.size()>0){
					employe = eList.get(0);
					employeNo =employe.getEmployeNo();
				}else{
					employeNo ="0";
					result.setStatus(false);
					result.setMessage("导购信息有误");
					log.error("导购信息有误");
					return result;
				}
			}
			paramMap.put("employeNo", employeNo);
			Map map = uEmployeMapper.getEmployeByNo(paramMap);
			if(map==null){
				result.setStatus(false);
				result.setMessage("导购信息有误");
				log.error("导购信息有误");
				return result;
			}
			result.setData(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}


	@Override
	public ResponseForm search(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			String storeNo =(String) paramMap.get("storeNo");
			String employeNo =(String) paramMap.get("employeNo");
			String wxUid =(String) paramMap.get("wxUid");
			if(!StringUtils.isNotBlank(storeNo)){
				result.setStatus(false);
				result.setMessage("门店编号有误");
				log.error("门店编号有误");
				return result;
			}
			if(!StringUtils.isNotBlank(employeNo)){
				UEmploye employe = new UEmploye();
				employe.setStoreNo(storeNo);
				employe.setRoleId(1);
				List<UEmploye> eList=uEmployeMapper.select(employe);
				if(eList!=null && eList.size()>0){
					employe = eList.get(0);
					employeNo =employe.getEmployeNo();
				}else{
					employeNo ="0";
					result.setStatus(false);
					result.setMessage("导购信息有误");
					log.error("导购信息有误");
					return result;
				}
			}
			com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
			List<Map<String, Object>> list =goodsMapper.searcheGoodsList(paramMap);;
			result.setData(list);
			result.setTotal((int) pageHelper.getTotal());
			//打点数据
			if(StringUtils.isNotBlank(wxUid)){
				//新增门店客户
				Map<String, Object> cMap = new HashMap<String, Object>();
				cMap.put("wxUid", wxUid);
				cMap.put("storeNo", storeNo);
				cMap.put("employeNo", employeNo);
				cMap.put("actionType", EventTag.NEWCUSTOMER);
				cMap.put("createTime", new Date());
				MQUtil.sendObMsg(cMap,jmsTemplate,topicDestination);
				//浏览首页
				//Map<String, Object> cMap = new HashMap<String, Object>();
				cMap.put("wxUid", wxUid);
				cMap.put("storeNo", storeNo);
				cMap.put("employeNo", employeNo);
				cMap.put("actionType", EventTag.SEARCH);
				cMap.put("eventTime", new Date());
				MQUtil.sendObMsg(cMap,jmsTemplate,topicDestination);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}



}
